package net.loyintean.pearlshell.validation.rule;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.loyintean.pearlshell.validation.IValidRuleConfig;

class StringAllChineseValidRuleImpl extends AbstractStringValidRuleImpl {

	StringAllChineseValidRuleImpl() {
		super();
	}

	private static final Pattern NOT_CHINESE_LEAST_ONCE = Pattern
			.compile("[^\\u4e00-\\u9fa5]+");

	private static final Pattern CHINESE_LEAST_ONCE = Pattern
			.compile("[\\u4e00-\\u9fa5]+");

	@Override
	public String errorMsg(IValidRuleConfig config) {
		return "只能包含汉字";
	}

	@Override
	protected boolean validString(String toValid, IValidRuleConfig config) {

		Matcher has_chinese = CHINESE_LEAST_ONCE.matcher(toValid);

		Matcher not_chinese = NOT_CHINESE_LEAST_ONCE.matcher(toValid);

		return has_chinese.find() && !not_chinese.find();
	}

}
